import dayjs from "dayjs"
import { message } from "ant-design-vue"
import { ResponsePromiseData } from "@/types"
import caseHttpRequest from "./oralCase"
import userHttpRequest from "./user"

// 获取内容统计数据
async function getStatisticalData<T>(): ResponsePromiseData<T> {
  try {
    // 案例类别
    let {
      data: { total: totalCategory }
    } = await caseHttpRequest.getCaseCategoryList<any>()
    // 全部案例数
    let {
      data: { total: totalCase }
    } = await caseHttpRequest.getCaseRecords<any>()
    // 今日新增案例数
    let {
      data: { total: totalCaseDay }
    } = await caseHttpRequest.getTodayCaseCount<any>()
    // 全部用户数
    let {
      data: { total: totalUser }
    } = await userHttpRequest.getUserList<any>()
    return {
      code: 0,
      data: {
        totalCategory,
        totalCase,
        totalCaseDay,
        totalUser
      } as T,
      message: "获取成功"
    }
  } catch (error: any) {
    message.error(error.message)
    return Promise.reject(error.message)
  }
}

// 获取最近一周的新增案例数据
async function getWeeklyCaseData<T>(): ResponsePromiseData<T> {
  const count = 7
  let weekData = new Array(count).fill(0).map((_el, idx) => ({
    x: dayjs()
      .subtract(count - idx - 1, "day")
      .format("YYYY-MM-DD"),
    y: 0
  }))
  try {
    for (let item of weekData) {
      let {
        data: { total }
      } = await caseHttpRequest.getTodayCaseCount<any>(item.x)
      item.y = total
    }
    return {
      code: 0,
      data: {
        weekData
      } as T,
      message: "获取成功"
    }
  } catch (error: any) {
    message.error(error.message)
    return Promise.reject(error.message)
  }
}

export default {
  getStatisticalData,
  getWeeklyCaseData
}
